package org.onejava.core.controller;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.onejava.core.bean.Allconfig;
import org.onejava.core.bean.InterceptorEntity;
import org.onejava.core.bean.MContext;
import org.onejava.core.exception.MMException;
import org.onejava.core.manage.InterceptorManage;
import org.onejava.core.manage.KActionManage;
import org.onejava.core.manage.ViewManage;
import org.onejava.core.manage.inter.ActionManage;
import org.onejava.core.tools.ActionTools;
import org.onejava.core.tools.ValuesTool;


public class ActionController {
	private static ActionController ac=null;
    private   Log log = LogFactory.getLog(ActionController.class);  

	private ActionController(){
	}
	public static ActionController getInstance(){
		if(ac==null){
			ac = new ActionController();
		}
		return ac;
		
	}
	
	public void actionDo(MContext context) {
		// TODO Auto-generated method stub
		try {
			prepare(context);
			work(context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("运行时的错误:", e);
		}
		try {
			view(context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("视图处理错误:", e);
		}

	}
	
	private  void prepare (MContext context) throws Exception{
		String classPath = ActionTools.getClassPath(context.getRequest().getServletPath());
		context.setClassPath(classPath.toLowerCase());
		context.setFiles();	
		context.setMap(ValuesTool.getValuesMap(context.getRequest().getParameterMap()));
		context.setResultMap(new HashMap());
	}
	
	/**
	 * @param context
	 * @throws Exception
	 */
	private void work(MContext context) throws Exception {
		//执行插件
		ActionManage am=null;
		am = KActionManage.getActionManage();		
		am.reloadAction(context.getClassPath());
		//校验路径是否有对应的Action
		String result="404";
		if(!am.actionIsExeit(context.getClassPath())){
			log.info("路径:"+context.getClassPath()+"未发现对应的Action");
			return;
		}
		
		InterceptorManage im = InterceptorManage.getManage();
		//前置拦截器链
		InterceptorEntity[] beforeChain =  am.getBeforeInterceptorChain(context.getClassPath());
		if(null!=beforeChain){
			for(int i=0;i<beforeChain.length;i++){
				try {
				String ieResult = (String) im.doInterceptor(beforeChain[i],context);
					if(null!=ieResult&&!"".equals(ieResult)){
						context.setResult(ieResult);
						return;
					}
				} catch (MMException e) {
					result="error";
					log.error("前置拦截器:<"+beforeChain[i].getName()+">执行错误",e.getCause());
					context.setResult(result);
					return;
				}
			}
		}
		try {
			result = (String) am.doAction(context.getClassPath(), Allconfig.defaultMethod, context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result="error";
			log.error("Action:<"+context.getClassPath()+">执行错误",e.getCause());
			context.setResult(result);
			return;			
		}
		context.setResult(result);
		//执行插件
		
		//后置拦截器链
		InterceptorEntity[] afterChain =  am.getAfterInterceptorChain(context.getClassPath());
		if(null!=afterChain){
			for(int i=0;i<afterChain.length;i++){
				try {
				String ieResult = (String) im.doInterceptor(afterChain[i],context);
					if(null!=ieResult&&!"".equals(ieResult)){
						context.setResult(ieResult);
						return;
					}
				} catch (MMException e) {
					result="error";
					log.error("后置拦截器:<"+afterChain[i].getName()+">执行错误",e.getCause());
					context.setResult(result);
					return;
				}
			}
		}
		//执行插件
	}
	
	private void view(MContext context) throws Exception{
		ViewManage vm = ViewManage.getMamage();
		vm.doView(context);
	}
	
	
}
